package net.qiqbframework.common.utils;

import net.qiqbframework.commandhandling.CommandHandler;
import net.qiqbframework.eventhandling.EventHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import static net.qiqbframework.common.utils.AnnotationUtil.isAnnotationPresent;


/**
 * 使用反射来是否有某种类型的 MessageHandler
 */
public class HandlerTypeResolver {

    /**
     * 判断类是否有 {@link CommandHandler} 注解的方法
     */
    public static boolean isCommandHandler(Class<?> handlerClass) {
        return isHandlerOfType(handlerClass, CommandHandler.class);
    }

    /**
     * 判断类是否有 {@link EventHandler} 注解的方法
     */
    public static boolean isEventHandler(Class<?> handlerClass) {
        return isHandlerOfType(handlerClass, EventHandler.class);
    }


    private static boolean isHandlerOfType(Class<?> handlerClass, Class<? extends Annotation> annotationType) {
        for (Method m : ReflectionUtil.methodsOf(handlerClass)) {
            if (isAnnotationPresent(m, annotationType)) {
                return true;
            }
        }
        return false;
    }

    private HandlerTypeResolver() {

    }
}
